<?php
////////////////////////////////////////////////////////////////////////////////
//	  This file is part of PushLMS.
//
//    PushLMS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    PushLMS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with PushLMS.  If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

class AnswersController extends AppController {

	var $scaffold = 'admin';

	var $helpers = array('Form', 'Javascript', 'JsValidate.Validation', 'Ajax');

	var $uses = array('Answer', 'Question', 'Questiongroup', 'Asession', 'Asessionlog', 'Assessment');

	function beforeFilter() {


	}

	################
	# POST UPDATE METHODS
	################

	public function reorder(){

		#error_log(print_r($this->data['answers'][0],true));

		$rank = 1;
		foreach ($this->data['answers'][0] as $answer_id){
			
			if (preg_match('/^[0-9]+$/',$answer_id)){
				#error_log("setting rank $rank for $answer_id");
				$this->Answer->id = $answer_id;
				$this->Answer->saveField('rank', $rank);
				$rank ++;
			}

		}

		$this->layout = 'none';

	}

	################
	# AJAX METHODS
	################

	public function ajax_get($id=null){

		if ($this->RequestHandler->isAjax()){

			#error_log('answer id after:'.$id);

			Configure::write('debug', 0);

			if ($id != null){

				$data = $this->Question->findById($id);
				$questiongroup = $this->Questiongroup->findById($data['Question']['questiongroup_id']);
				$assessment = $this->Assessment->findById($questiongroup['Questiongroup']['assessment_id']);
				$this->set('question', $data);
				$this->set('assessment',$assessment);
				$this->set('questiongroup',$questiongroup);
				$this->Session->setFlash("");

			}
	
		}

	}

	public function ajax_edit($id=null){

		if ($this->RequestHandler->isAjax()){
			if(!empty($this->data)) {

				//#error_log('answer:'.$id);
				//Configure::write('debug', 0);

				if($this->Answer->save($this->data)) {
					$this->Session->setFlash('');
					$id = $this->Answer->id;
					$tmpanswer = $this->Answer->findById($id);
					$tmpquestion = $this->Question->findById($tmpanswer['Answer']['question_id']);
					$this->redirect('/answers/ajax_get/'.$tmpquestion['Question']['id']);
					$this->set('flash', true);
				}
				else
				{
					//grab the id in order to keep the breadcrum intact
					$id = $this->data['Answer']['id'];
				}
			}
			$answer = $this->Answer->findById($id);
			$question = $this->Question->findById($answer['Answer']['question_id']);
			$questiongroup = $this->Questiongroup->findById($question['Question']['questiongroup_id']);
			$assessment = $this->Assessment->findById($questiongroup['Questiongroup']['assessment_id']);
			
			$this->set('assessment',$assessment);
			$this->set('questiongroup',$questiongroup);
			$this->set('question',$question);
			$this->set('answer',$answer);
			$this->data = $answer;

		}
	}


	public function ajax_delete($id=null){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if ($id != null){

				$answer = $this->Answer->findById($id);
				$this->Answer->delete($id, true);
				$this->ajax_get($answer['Answer']['question_id']);
				
				$question = $this->Question->findById($answer['Answer']['question_id']);
				$questiongroup = $this->Questiongroup->findById($question['Question']['questiongroup_id']);
				$assessment = $this->Assessment->findById($questiongroup['Questiongroup']['assessment_id']);
				
				$this->set('assessment',$assessment);
				$this->set('questiongroup',$questiongroup);
				$this->set('question',$question);

			}
		}
	}

	public function ajax_add($question_id=null){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if ($question_id != null){

				#error_log('question id:'.$question_id);
	
				$newAnswer = array(
					'Answer' => array(
						'answer_text' => 'new-answer',
						'answer_int' => 0,
						'answer_decimal' => 0.00,
						'answer_image' => null,
						'question_id' => $question_id
					),
				);

				$this->Answer->save($newAnswer);

				#error_log('new answerd:'.$this->Answer->id);

				//$question = $this->Question->findById(question_id);
				$this->ajax_get($question_id);
				
				$question = $this->Question->findById($question_id);
				$questiongroup = $this->Questiongroup->findById($question['Question']['questiongroup_id']);
				$assessment = $this->Assessment->findById($questiongroup['Questiongroup']['assessment_id']);
				
				$this->set('assessment',$assessment);
				$this->set('questiongroup',$questiongroup);
				$this->set('question',$question);

			}
	
		}

	}

	public function ajax_duplicate($id=null, $question_id=false){

		if ($this->RequestHandler->isAjax()){

			Configure::write('debug', 0);

			if (!empty($id)) {
				
				$this->Answer->copy($id);

				if ($question_id){
					#error_log('question_id:'.$question_id);
					$this->ajax_get($question_id);
				}

			}

		}

	}



}

?>
